home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / JButtonGroupPanel.java < prev    next >
Text File  |  1998-10-12  |  4KB  |  156 lines

  1. package com.symantec.itools.swing;
  2.  
  3.  
  4. import java.awt.Component;
  5. import java.awt.Dimension;
  6. import java.awt.GridLayout;
  7. import java.awt.LayoutManager;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.util.Enumeration;
  11. import java.util.Vector;
  12. import com.sun.java.swing.AbstractButton;
  13. import com.sun.java.swing.ButtonGroup;
  14. import com.sun.java.swing.ButtonModel;
  15. import com.sun.java.swing.JPanel;
  16.  
  17.  
  18. public class JButtonGroupPanel
  19.     extends    JPanel
  20.     implements ActionListener
  21. {
  22.     protected Vector      buttons;
  23.     protected Vector      listeners;
  24.     protected ButtonGroup group;
  25.  
  26.     {
  27.         buttons   = new Vector();
  28.         group     = new ButtonGroup();
  29.         listeners = new Vector();
  30.     }
  31.     
  32.     public JButtonGroupPanel()
  33.     {
  34.         super.setLayout(new GridLayout());
  35.     }
  36.     
  37.     public JButtonGroupPanel(ButtonGroup grp)
  38.     {
  39.         for (Enumeration e = grp.getElements(); e.hasMoreElements();)
  40.         {
  41.             add((AbstractButton)e.nextElement());
  42.         }
  43.     }
  44.     
  45.     public Enumeration getElements()
  46.     {
  47.         return (group.getElements());
  48.     }
  49.     
  50.     public void addActionListener(ActionListener listener)
  51.     {
  52.         if (!(listeners.contains(listener)))
  53.             listeners.addElement(listener);
  54.     }
  55.     
  56.     public void removeActionListener(ActionListener listener)
  57.     {
  58.         if (listeners.contains(listener))
  59.             listeners.removeElement(listener);
  60.     }
  61.     
  62.     public void actionPerformed(ActionEvent event)
  63.     {
  64.         Vector vector;
  65.         ActionEvent evt;
  66.  
  67.         synchronized(listeners)
  68.         {
  69.             vector = (Vector)listeners.clone();
  70.         }
  71.  
  72.         evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, ((AbstractButton)event.getSource()).getText());
  73.  
  74.         for(Enumeration e = vector.elements(); e.hasMoreElements();)
  75.         {
  76.             ((ActionListener)e.nextElement()).actionPerformed(evt);
  77.         }
  78.     }
  79.  
  80.     protected void addImpl(Component comp, Object constraints, int index)
  81.     {
  82.         if (comp instanceof AbstractButton)
  83.         {
  84.             AbstractButton btn = (AbstractButton)comp;
  85.             
  86.             if (!(buttons.contains(btn)))
  87.             {
  88.                 buttons.addElement(btn);
  89.                 group.add(btn);
  90.                 btn.addActionListener(this);
  91.                 super.addImpl(comp, constraints, index);
  92.             }
  93.         }
  94.         else super.addImpl(comp, constraints, index);
  95.     }
  96.  
  97.     public void remove(AbstractButton btn)
  98.     {
  99.         if(buttons.contains(btn))
  100.         {
  101.             buttons.removeElement(btn);
  102.             group.remove(btn);
  103.         }
  104.     }
  105.  
  106.     public void setSelection(AbstractButton btn, boolean f)
  107.     {
  108.         btn.getModel().setSelected(f);
  109.     }
  110.  
  111.     public void setSelection(String text, boolean f)
  112.     {
  113.         for(Enumeration e = buttons.elements(); e.hasMoreElements();)
  114.         {
  115.             AbstractButton btn;
  116.  
  117.             btn = (AbstractButton)e.nextElement();
  118.  
  119.             if(btn.getText().equals(text))
  120.             {
  121.                 setSelection(btn, f);
  122.             }
  123.         }
  124.     }
  125.  
  126.     public AbstractButton getSelection()
  127.     {
  128.         ButtonModel model;
  129.  
  130.         model = group.getSelection();
  131.  
  132.         for(Enumeration e = buttons.elements(); e.hasMoreElements();)
  133.         {
  134.             AbstractButton btn;
  135.  
  136.             btn = (AbstractButton)e.nextElement();
  137.  
  138.             if(btn.getModel() == model)
  139.             {
  140.                 return (btn);
  141.             }
  142.         }
  143.  
  144.         return (null);
  145.     }
  146.  
  147.     public boolean isSelected(AbstractButton btn)
  148.     {
  149.         return (btn == getSelection());
  150.     }
  151.  
  152.     public boolean isSelected(String text)
  153.     {
  154.         return (text.equals(getSelection().getText()));
  155.     }
  156. }